package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.UserMapper;
import com.model.User;
import com.service.UserService;
import com.util.Result;
import com.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@CrossOrigin("*")
public class UserLoginAndRegisterController {
    @Autowired
    private UserMapper userDao;

    @Autowired
    private UserService userService;

    /**
     * 用户注册接口
     */
    @PostMapping("/userRegister")
    public Result register(@RequestBody User user) {
        if (userService.selectCount(user.getAccount(),user.getPhone())) {
            return new Result("500","账号或手机号已存在",0,null);
        }
        user.setCreateTime(new Date());
        userService.save(user);
        return new Result("200","注册成功",1,null);
    }

    /**
     * 用户登录接口
     */
    @PostMapping("/userLogin")
    public Result login(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account", user.getAccount()).eq("password", user.getPassword());
        User loginUser = userService.getOne(wrapper);
        if (loginUser != null){
            return new Result("200","登录成功",1,null);
        }else {
            return new Result(ResultEnum.USERNAME_PASSWORD_ERROR,0,null);
        }
    }
}
